import Ember from 'ember';
import BaseItem from '../ui/base-ui-item';
import Changeset from 'ember-changeset';
import CustomerbusinessflowValidations from '../../validations/customerbusinessflow';
import lookupValidator from 'ember-changeset-validations';

export default BaseItem.extend(CustomerbusinessflowValidations,{
  store: Ember.inject.service('store'),
  dataLoader: Ember.inject.service("data-loader"),
  dateService: Ember.inject.service("date-service"),
  constants: Constants,
  defaultBed:Ember.computed('customerbusinessflowInfo.orderBed',function(){
      return this.get('customerbusinessflowInfo.orderBed');
  }),
  defaultStaff:Ember.computed('customerbusinessflowInfo.orderStaff',function(){
      return this.get('customerbusinessflowInfo.orderStaff');
  }),
  customerbusinessflowModel:Ember.computed('customerbusinessflowInfo',function(){
    let model = this.get("customerbusinessflowInfo");
    if (!model) {
        return null;
    }
    return new Changeset(model, lookupValidator(CustomerbusinessflowValidations), CustomerbusinessflowValidations);
  }),

  actions:{
    invalid() {
        //alert("error");
    },
    //保存
    detailSaveClick: function() {

      let editMode=this.get('editMode');
      let customerbusinessflowModel=this.get('customerbusinessflowModel');
      let customerbusinessflowInfo = this.get('customerbusinessflowInfo');
      let customer = this.get('customer');
      let _self=this;
      let goBack = this.get('goBack');
      let status = this.get("dataLoader").findDict("consultStatus3");
        customerbusinessflowModel.validate().then(function(){
          let curBed = _self.get('store').peekRecord('bed',customerbusinessflowInfo.get('orderBed.id'));
          let bedStatus = _self.get("dataLoader").findDict("bedStatusAppointment");
          curBed.set('status',bedStatus);
          curBed.save();
          customer.set('name',customerbusinessflowModel.get('cusName'));
          customer.set('sex',customerbusinessflowModel.get('cusSex'));
          customer.set('selfCareAbility',customerbusinessflowModel.get('cusCare'));
          customer.set('cardCode',customerbusinessflowModel.get('cusCardCode'));
          customer.set('customerNative',customerbusinessflowModel.get('cusNative'));
          customer.set('educationLevel',customerbusinessflowModel.get('cusEdu'));
          customer.save().then(function(customer){
            customerbusinessflowInfo.set('customer',customer);
            customerbusinessflowInfo.set('status',status);
            customerbusinessflowInfo.set('advName',_self.get('customerbusinessflowModel.advName'));
            customerbusinessflowInfo.set('advTel',_self.get('customerbusinessflowModel.advTel'));
            customerbusinessflowInfo.set('orderRemark',_self.get('customerbusinessflowInfo.orderRemark'));
            customerbusinessflowInfo.set('orderInTime',customerbusinessflowModel.get('orderInTime'));
            customerbusinessflowInfo.set('orderDate',customerbusinessflowModel.get('orderDate'));
            customerbusinessflowInfo.set('orderMoney',customerbusinessflowModel.get('orderMoney'));

            if(customerbusinessflowModel.get('errors.length')===0){

              App.lookup('controller:business.mainpage').openPopTip("正在保存");
              console.log(customerbusinessflowInfo.get('orderBed.id'));

              customerbusinessflowInfo.save().then(function(){
                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                if(editMode=='add'){

                  let mainpageController = App.lookup('controller:business.mainpage');
                  console.log('goBack:',goBack);
                  if(goBack=='scheduled'){
                    mainpageController.switchMainPage('business-customer');
                  }else{
                    _self.sendAction('changeStatus');
                    mainpageController.switchMainPage('consultation-management',{saveConsult:true});
                  }

                }else{
                    _self.set('detailEdit',false);
                }
            });
          }else{
            customerbusinessflowModel.set("validFlag",Math.random());
          }
          });


        });

    },
    //编辑按钮
    detailEditClick:function(){
      this.set('detailEdit',true);
    },
    //取消
    detailCancel:function(){
      let id=this.get('id');
      let editMode=this.get('editMode');
      let goBack = this.get('goBack');
      if(id&&editMode=='edit'){
        this.set('detailEdit',false);
        this.get('customerbusinessflowInfo').rollbackAttributes();
      }else{
        let mainpageController = App.lookup('controller:business.mainpage');
        if(goBack=='scheduled'){
          mainpageController.switchMainPage('business-customer');
        }else{
          mainpageController.switchMainPage('consultation-management');
        }


      }

    },
    //选择床位
    selectBed(Bed) {
        this.set('customerbusinessflowInfo.orderBed',Bed);
        this.set('customerbusinessflowModel.orderBed',Bed);
        console.log('name:',this.get('goBack'));
    },
    //选择办理人
    selectStaff(staff){
      this.set('customerbusinessflowInfo.orderStaff',staff);
      this.set('customerbusinessflowModel.orderStaff',staff);
    },
    //选择性别
    sexSelect(sex){
      console.log('customerSex:',this.get('customer.sex'),sex.get('typename'));
      this.set('customer.sex',sex);
      this.set('customerbusinessflowModel.cusSex',sex);
    },
    //选择籍贯
    nativeSelect(native){
      this.set('customer.customerNative',native);
      this.set('customerbusinessflowModel.cusNative',native);
    },
    //选择自理能力
    abilitySelect(abilityDict){
      this.set('customer.selfCareAbility',abilityDict);
      this.set('customerbusinessflowModel.cusCare',abilityDict);
    },
    //选择教育级别
    educationSelect(educationDict){
      this.set('customer.educationLevel',educationDict);
      this.set('customerbusinessflowModel.cusEdu',educationDict);
    },
    //选择入院日期
    changeOrderInTimeAction(date){
      //当前选择时间
      let stamp = this.get("dateService").timeStringToTimestamp(date);
      this.set("customerbusinessflowModel.orderInTime", stamp);
    },
    //选择预定办理日期
    changeOrderDateAction(date){
      let stamp = this.get("dateService").timeStringToTimestamp(date);
      console.log("stamp",stamp);
      this.set("customerbusinessflowModel.orderDate", stamp);
    },
    dpShowAction(){},

  }
});
